package com.tzbank.esb.util;

import com.dcfs.esb.ftp.server.error.FtpException;
import com.dcfs.esb.ftp.server.scrt.ScrtUtil;
import com.dcfs.esb.ftp.server.socket.FtpConnector;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

@Slf4j
@Data
public class FtpClientConfigs {
    private static String serverIp = "192.168.53.207";
    private static int port = 5001;
    private static String uid = "IFE";
    private static String passwd = "IFE";


    public static String getServerIp() {
        return serverIp;
    }

    public static void setServerIp(String serverIp) {
        serverIp = serverIp;
    }

    public static int getPort() {
        return port;
    }

    public static void setPort(int port) {
        port = port;
    }

    public static String getUid() {
        return uid;
    }

    public static void setUid(String uid) {
        uid = uid;
    }

    public static String getPasswd() {
        return passwd;
    }

    public static void setPasswd(String passwd) {
        passwd = passwd;
    }

    public static FtpConnector getConnector() throws FtpException {
        FtpConnector conn = new FtpConnector(serverIp, port);
        return conn;
    }

    public static void loadConf(Environment environment) {

        String ip = environment.getProperty("serverIp", String.class);
        setServerIp(ip);
        int pt = environment.getProperty("port", Integer.class);
        setPort(pt);
        String ur = environment.getProperty("uid", String.class);
        setUid(ur);
        String pw = environment.getProperty("passwd", String.class);
        if (pw.startsWith("${3DES}")) {
            pw = ScrtUtil.decryptEsb(pw.substring(7));
            setPasswd(pw);
        } else {
            setPasswd("${3DES}" + ScrtUtil.encryptEsb(pw));
        }

    }

}